package com.situ.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";

    private Button mBtn1;
    private Button mBtn2;
    private Button mBtn3;

    // 生命周期方法
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 绑定Layout界面
        setContentView(R.layout.activity_main);

        // 去layout里面找到控件
        mBtn1 = findViewById(R.id.btn1);
        mBtn2 = findViewById(R.id.btn2);
        mBtn3 = findViewById(R.id.btn3);

        mBtn2.setOnClickListener(this);
        mBtn3.setOnClickListener(this);

        // 绑定点击事件
        mBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"触发了点击事件", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(MainActivity.this, TestActivity.class);
                startActivity(intent);
            }

        });

        System.out.println("MainActivity.onCreate");
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
        }
        show();
        System.out.println("MainActivity.onCreate");
    }

    private void show() {
        System.out.println("MainActivity.show");
    }

    // onClick
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn2:
                Toast.makeText(getApplicationContext(), "您点击了按钮2", Toast.LENGTH_LONG).show();
                // System.out.println();
                Log.d(TAG, "debug");
                Log.i(TAG, "info");
                Log.w(TAG, "warn");
                Log.e(TAG, "error");

                
                break;
            case R.id.btn3:
                Toast.makeText(getApplicationContext(), "您点击了按钮3", Toast.LENGTH_LONG).show();
                AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("Dialog确认框")
                        .setMessage("您是否要退出")
                        .setIcon(R.mipmap.ic_launcher)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(), "您点击了确认", Toast.LENGTH_LONG).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(), "您点击了取消", Toast.LENGTH_LONG).show();
                            }
                        })
                        .create();
                dialog.show();
                break;
        }

    }
}